import React, { useEffect } from 'react';
import { Routes, Route, Navigate, useNavigate } from 'react-router-dom';
import { notification } from 'antd';

import Login from '@/pages/login';
import Layout from '@/layout';
import Dashboard from '@/pages/dashboard';
import HospitalList from '@/pages/doctorse/hospitallist';
import DoctorList from '@/pages/doctorse/doctorlist';
import EventBus from '@/utils/event';
import Register from '@/pages/register';
import User from '@/pages/user';
import Managerlist from '@/pages/managers/managerlist';
import Permissionlist from '@/pages/managers/permissionlist';
import Rolelist from '@/pages/managers/rolelist';

const App: React.FC = () => {
  const navigate = useNavigate();

  const openNotification = (msg: string) => {
    notification.error({
      message: '发生错误',
      description: `错误信息：${msg}`,
      duration: 4,
    });
  };
  useEffect(() => {
    EventBus.on('global_not_login', (msg) => {
      navigate('/login');
      openNotification(msg);
    });
  }, []);
  return (
    <>
      <Routes>
        <Route path='/' element={<Navigate to={'/login'} />}></Route>
        <Route path='/login' element={<Login />}></Route>
        <Route path='/register' element={<Register />}></Route>
        <Route element={<Layout />}>
          <Route path='/dashboard' element={<Dashboard />}></Route>
          <Route path='/users' element={<User />}></Route>
          <Route path='/hospitallist' element={<HospitalList />}></Route>
          <Route path='/doctorlist' element={<DoctorList />}></Route>

          <Route path='/manager' element={<Managerlist />} />
          <Route path='/permission' element={<Permissionlist />} />
          <Route path='/role' element={<Rolelist />} />
        </Route>
      </Routes>
    </>
  );
};

export default App;
